package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.RoundClickAdapter;
import com.sctu.bean.RoundBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fll on 2015/12/18.
 */
public class ScenictourRoundClickAvtivity extends Activity {

    private TextView title, content;
    private ImageView back, imageView;
    private ListView listView;
    private List<RoundBean.Results> resultsList = new ArrayList<RoundBean.Results>();
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scenictour_round_after_click);

        initView();
    }

    public void initView() {

        title = (TextView) findViewById(R.id.round_after_click_title);
        content = (TextView) findViewById(R.id.round_after_click_charac);
        back = (ImageView) findViewById(R.id.round_after_click_back_button);
        imageView = (ImageView) findViewById(R.id.round_click_img);
        listView = (ListView) findViewById(R.id.round_list);
        linearLayout = (LinearLayout) findViewById(R.id.round_click_linear);

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) linearLayout.getLayoutParams();
        linearParams.height = (int) (height*0.33);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        GetVolleyHttp.getInstance(ScenictourRoundClickAvtivity.this).getStringGETJsonData(UrlConfig.URL_SCENIC_ROUND_ITEM + id,
                listener, errorlistener);
    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {

            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final RoundBean resultslist = JSON.parseObject(jsonstring, RoundBean.class);
            resultsList = resultslist.getResults();
            //解析图片
            GetVolleyHttp.getInstance(ScenictourRoundClickAvtivity.this).setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + resultsList.get(0).getPath());
            //建立适配器，并绑定数据源绑定适配器到listview
            listView.setAdapter(new RoundClickAdapter(resultsList, ScenictourRoundClickAvtivity.this));

            title.setText(resultsList.get(0).getOrder());
            content.setText(resultsList.get(0).getCharacteristic());

        }

    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(ScenictourRoundClickAvtivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };


}
